package com.gouaixin.bee.beeframework2.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.util.AbToastUtil;
import com.ab.view.titlebar.AbTitleBar;
import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.adapter.ServiceMsgAdapter;
import com.gouaixin.bee.beeframework2.bean.ServiceMsg;
import com.gouaixin.bee.beeframework2.dao.ServiceMsgDao;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.bmob.v3.BmobPushManager;

/**
 * Created by Administrator on 2015/3/10.
 */
//缘分小秘书聊天消息界面（使用数据库）
public class C2_ServiceMsgActivity extends AbActivity {
    Context context = C2_ServiceMsgActivity.this;

    String jid = "聊天界面";
    String cid = "";
    String cname = "";
    String flag = "";

    ImageView back;//返回按钮
    TextView title;//标题姓名
    ImageView right;//标题姓名

    EditText input;//输入框
    TextView send;//发送按钮

    ListView list;//聊天列表
    List<ServiceMsg> datas;
    ServiceMsgAdapter adapter;

    RelativeLayout footer;
    RelativeLayout suoyao;

    String jicon = "";//保存的头像网址
    String jname = "";//机器人name
    String qianming = "";//机器人的内心独白
    BmobPushManager bmobPushManager;


    //标题栏
    private AbTitleBar mAbTitleBar = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.c2_servicemsg);
        datas = new ArrayList<ServiceMsg>();
        bmobPushManager = new BmobPushManager(this);
        adapter = new ServiceMsgAdapter(context);
        list = (ListView) findViewById(R.id.list);
        list.setAdapter(adapter);

//        initIntent();
        initHead();
        initFoot();

    }


    @Override
    protected void onPause() {
        super.onPause();
        if (flag.equals("0")) {
            PreferencesUtils.putInt(context, "liaoStatus", 0);

        }
    }

    @Override
    protected void onResume() {
        super.onResume();

//        /***
//         * liaoStatus（聊天状态）：0（程序在后台）；1（程序在前端显示，非聊天界面）；2（显示聊天界面）
//         */
//        PreferencesUtils.putInt(context, "liaoStatus", 2);
//        int liaoStatus = PreferencesUtils.getInt(context, "liaoStatus");
//        mLogUtil.e("当前的聊天状态：liaoStatus=" + liaoStatus);


        mLog.e("恢复运行时读取的聊天记录：");
        datas = ServiceMsgDao.query(context);
        adapter.setLists(datas);
        adapter.setIcon(jicon);
        adapter.notifyDataSetChanged();
        if (datas != null) {
//            list.setSelection(datas.size());
            mLog.e(" list.setSelection(datas.size())=" + new Gson().toJson(datas));
        }


    }



    /**
     * 改进的发送消息的方法
     */
    private void send() {
            //1..封装消息
            final String msg = input.getText().toString();
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            final ServiceMsg bean = new ServiceMsg();

            bean.setMsg(msg);
            bean.setTime(time);
            bean.setType(ServiceMsg.SEND);

            //2.配置其他
            datas.add(bean);
            adapter.setLists(datas);
            adapter.notifyDataSetChanged();
            list.setSelection(datas.size());
            input.setText("");

        mLog.e("调用消息控制器，发送系统消息：msg=" + new Gson().toJson(bean));

            //调用消息控制器，发送消息
//            ServiceMsgDao.add(context, bean);

//
//        mLogUtil.e("调用消息控制器，读取系统消息：msg=");
//        ServiceMsgDao.query(context);
    }



//
//    String serviceMsg="";
//    private void initIntent() {
//
////        Intent it=getIntent();
////        serviceMsg=it.getStringExtra("text");
////        if(serviceMsg.endsWith("")){
////            mLogUtil.e("initIntent.serviceMsg="+serviceMsg);
////            cid = PreferencesUtils.getString(context, "cid");
////            cname = PreferencesUtils.getString(context, "cname");
////            ServiceChatModel.receiveServiceMsg(context,serviceMsg);
////        }
//
//    }


    /**
     * 头部的监听
     */
    private void initHead() {
        //顶部
        back = (ImageView) findViewById(R.id.back);
        title = (TextView) findViewById(R.id.title);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }




    /**
     * 脚部的监听
     */
    boolean isFirstLiao;//是否第一次聊天

    private void initFoot() {
        footer = (RelativeLayout) findViewById(R.id.footer);

        footer.setVisibility(View.GONE);

        input = (EditText) findViewById(R.id.input);
        send = (TextView) findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String msg = input.getText().toString();

//                AbToastUtil.showToast(context, "系统红娘正在开发中……"+msg);


                if (msg.equals("")) {
                    AbToastUtil.showToast(context, "聊天内容不能为空");
                    return;
                }
                send();
            }
        });
    }





}
